<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+
      
class MemberModel extends AppModel
{

	var $tbl_pref_custom = '';
	var $tables = array('table'=>'user', 'user', 'role');
	var $custom_tables = array('entry'=>'kb_entry');
	
	
	function getRecordsSql() { 
		$sql = "
		SELECT 
			m.*, 
			r.title AS role, 
			UNIX_TIMESTAMP(date_registered) AS ts
		FROM {$this->tbl->user} m
		LEFT JOIN {$this->tbl->role} r ON m.role_id = r.id
		WHERE {$this->sql_params}
		{$this->sql_params_order}";
		
		return $sql;
	}
	
	
	function getRolesSelectRange() {
		$r = new RoleModel;
		return $r->getSelectRange('member');
	}	
	
	
	function save(&$obj) {
		
		if(!$obj->get('id')) {
			$user_id = $this->add($obj);
			
		} else {
			$this->update($obj);
			$user_id = $obj->get('id');
		}
        return $user_id;
	}	

	// VALIDATING // -------------------------
	
	// return true if we already have such username false otherwise
	function isUsernameExists($username, $id = false) {
		
		$cond = ($id) ? "id != '$id'" : "1=1"; 
		
		$sql = "SELECT 1 FROM {$this->tbl->user} WHERE username = '$username' AND $cond";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return (bool) ($result->Fields(1));
	}
	
	
	// return true if we already have such email false otherwise
	function isEmailExists($email, $id = false) {
		
		$cond = ($id) ? "email != '$email'" : "1=1";
		
		$sql = "SELECT 1 FROM {$this->tbl->user} WHERE email = '$email' AND $cond";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return (bool) ($result->Fields(1));
	}
	
	
	
	// DELETE RELATED // ---------------------
	
}
?>
